package com.woniuxy.carlife.carlifegateway.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;


/**
 * @author zjq
 * @ClassName:JwtUtil
 * @Description:jwt
 * @date 2021/4/6 13:54
 **/

@Component
public class GatewayJwtUtil {
    //签名

    //    普通用户签名
    private String ordinarySign = "laisdjfoiwqjflksaasfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";

    //    platformmanage 平台
    private String platformmanageSign = "fhggfdsacdghtyrfde45gtrfddertyhgtfdswer55";

    //            merchantmanage 商家
    private String merchantmanageSign = "lkjhghidwyuew8d7yg3hjewdyug3ew32owi9de23whj";

    //过期时间

    private Integer rdinaryExpire = 3;

    private Integer platformmanageExpire = 3;

    private Integer merchantmanageExpire = 3;


    /**
     * 功能描述〈普通用户生成SecretKey〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: javax.crypto.SecretKey
     * @Date 2021/4/6 13:54
     */

    public SecretKey ordinaryGetKey() {

        return Keys.hmacShaKeyFor(ordinarySign.getBytes());
    }


    /*
     * 功能描述〈平台生成SecretKey〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/9 9:23
     * @throws
     */

    public SecretKey platformmanageGetKey() {

        return Keys.hmacShaKeyFor(platformmanageSign.getBytes());
    }


    /*
     * 功能描述〈商家生成SecretKey〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/9 9:24
     * @throws
     */

    public SecretKey merchantmanageGetKey() {

        return Keys.hmacShaKeyFor(merchantmanageSign.getBytes());
    }


    /**
     * 功能描述〈普通用户生成token〉
     *
     * @throws
     * @author:zjq
     * @param: [maps]
     * @return: java.lang.String
     * @Date 2021/4/6 13:53
     */

    public String ordinaryGetToken(Map<String, Object> maps) {
        JwtBuilder jwtBuilder = Jwts.builder().signWith(ordinaryGetKey(), SignatureAlgorithm.HS256);
        if (!ObjectUtils.isEmpty(maps)) {
            jwtBuilder.setClaims(maps);
        }
        String token = jwtBuilder.setIssuedAt(new Date()).
                setExpiration(new Date(System.currentTimeMillis() + rdinaryExpire * 60 * 60 * 1000))
                .compact();
        return token;
    }

    /**
     * 功能描述〈平台生成token〉
     *
     * @throws
     * @author:zjq
     * @param: [maps]
     * @return: java.lang.String
     * @Date 2021/4/6 13:53
     */

    public String platformmanageGetToken(Map<String, Object> maps) {
        JwtBuilder jwtBuilder = Jwts.builder().signWith(platformmanageGetKey(), SignatureAlgorithm.HS256);
        if (!ObjectUtils.isEmpty(maps)) {
            jwtBuilder.setClaims(maps);
        }
        String token = jwtBuilder.setIssuedAt(new Date()).
                setExpiration(new Date(System.currentTimeMillis() + platformmanageExpire * 60 * 60 * 1000))
                .compact();
        return token;
    }


    /**
     * 功能描述〈商家生成token〉
     *
     * @throws
     * @author:zjq
     * @param: [maps]
     * @return: java.lang.String
     * @Date 2021/4/6 13:53
     */

    public String merchantmanageGetToken(Map<String, Object> maps) {
        JwtBuilder jwtBuilder = Jwts.builder().signWith(merchantmanageGetKey(), SignatureAlgorithm.HS256);
        if (!ObjectUtils.isEmpty(maps)) {
            jwtBuilder.setClaims(maps);
        }
        String token = jwtBuilder.setIssuedAt(new Date()).
                setExpiration(new Date(System.currentTimeMillis() + merchantmanageExpire * 60 * 60 * 1000))
                .compact();
        return token;
    }

    /**
     * 功能描述〈普通用户获取token〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: java.lang.String
     * @Date 2021/4/6 13:53
     */

    public String ordinaryGetToken() {
        return ordinaryGetToken(null);
    }

    /**
     * 功能描述〈平台获取token〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: java.lang.String
     * @Date 2021/4/6 13:53
     */

    public String platformmanageGetToken() {
        return platformmanageGetToken(null);
    }


    /**
     * 功能描述〈商家获取token〉
     *
     * @throws
     * @author:zjq
     * @param: []
     * @return: java.lang.String
     * @Date 2021/4/6 13:53
     */

    public String merchantmanageGetToken() {
        return merchantmanageGetToken(null);
    }


    /**
     * 功能描述〈普通用户解析token〉
     *
     * @throws
     * @author:zjq
     * @param: [token]
     * @return: io.jsonwebtoken.Claims
     * @Date 2021/4/6 13:54
     */

    public Claims parseTokenOrdinary(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(ordinaryGetKey()).build()
                .parseClaimsJws(token).getBody();
    }


    /**
     * 功能描述〈平台解析token〉
     *
     * @throws
     * @author:zjq
     * @param: [token]
     * @return: io.jsonwebtoken.Claims
     * @Date 2021/4/6 13:54
     */

    public Claims parseTokenPlatformmanage(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(platformmanageGetKey()).build()
                .parseClaimsJws(token).getBody();
    }

    /**
     * 功能描述〈商家解析token〉
     *
     * @throws
     * @author:zjq
     * @param: [token]
     * @return: io.jsonwebtoken.Claims
     * @Date 2021/4/6 13:54
     */

    public Claims parseTokenMerchantmanage(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(merchantmanageGetKey()).build()
                .parseClaimsJws(token).getBody();
    }
}
